package com.suse.service.community.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HtmlUtil;
import com.suse.common.util.extra.DesensitizationUtil;
import com.suse.service.community.entity.Message;
import com.suse.service.community.mapper.MessageMapper;
import com.suse.service.community.service.MessageService;
import com.suse.service.community.util.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息业务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-09-01 16:04
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Override
    public List<Message> getConversationList(int userId, int offset, int limit) {
        return messageMapper.selectConversationList(userId,offset,limit);
    }

    @Override
    public int getConversationTotal(int userId) {
        return messageMapper.selectConversationTotal(userId);
    }

    @Override
    public List<Message> getLetterList(String conversationId, int offset, int limit) {
        return messageMapper.selectLetterList(conversationId,offset,limit);
    }

    @Override
    public int getLetterTotal(String conversationId) {
        return messageMapper.selectLetterTotal(conversationId);
    }

    @Override
    public int getConversationUnreadLetterCount(int userId,String conversationId) {
        return messageMapper.selectConversationUnreadLetterCount(userId,conversationId);
    }

    @Override
    public int getUnreadLetterCount() {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectUnreadLetterCount(userId);
    }

    @Override
    public boolean save(Message message) {
        message.setContent(HtmlUtil.escape(message.getContent()));
        message.setContent(DesensitizationUtil.desensitize(message.getContent()));
        message.setCreateTime(new Date());
        message.setStatus(0);
        int row = messageMapper.insert(message);
        return row == 1;
    }

    @Override
    public boolean readLetterList(List<Integer> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return true;
        }
        int row = messageMapper.updateStatus(idList, 1);
        return row == idList.size();
    }

    @Override
    public Message getLatestTopicNotice(String topic) {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectLatestNotice(userId,topic);
    }

    @Override
    public int getTopicNoticeCount(String topic) {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectNoticeCount(userId,topic);
    }

    @Override
    public int getUnreadTopicNoticeCount(String topic) {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectUnreadTopicNoticeCount(userId,topic);
    }

    @Override
    public int getUnreadNoticeCount() {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectUnreadNoticeCount(userId);
    }

    @Override
    public List<Message> getTopicNoticeList(String topic, int offset, int limit) {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectTopicNoticeList(userId, topic, offset, limit);
    }

    @Override
    public int getUnreadMessageCount() {
        Integer userId = UserHolder.getUser().getId();
        return messageMapper.selectUnreadMessageCount(userId);
    }

}
